var prefix = "/yw/leave";
var prefix_1 = "/yw/appraisalUser";

$(document).ready(function () {
    validateRule();
    selectUser();
    selectLoad();
    createDate();
});

function createDate() {
    laydate.render({
        elem: '#leaveStart'
        , type: 'datetime'
    });
    laydate.render({
        elem: '#leaveEnd'
        , type: 'datetime'
    });
}

$.validator.setDefaults({
    submitHandler: function () {
        if (!$('#userId').val()) {
            parent.layer.msg("未选择用户！");
            return;
        }
        if (!$('#leaveType').val()) {
            parent.layer.msg("未选择请假类型！");
            return;
        }
        save();
    }
});

function selectUser() {
    var html = "";
    $.ajax({
        url: prefix_1 + '/list/appraisalUser',
        success: function (data) {
            //加载数据
            for (var i = 0; i < data.length; i++) {
                html += '<option value="' + data[i].userId + '">' + data[i].name + '</option>'
            }
            $("#userId").append(html);
            $("#userId").chosen({
                maxHeight: 200
            });

            //点击事件
            $('#userId').on('change', function () {
                $('#userName').val($('#userId').find('option:selected').text());
            });
        }
    });
}

function selectLoad() {
    var html = "";
    $.ajax({
        url: prefix + '/leaveType',
        success: function (res) {
            if (res.code === 0) {
                var data = res.data;
                //加载数据
                for (var i = 0; i < data.length; i++) {
                    html += '<option value="' + data[i].value + '">' + data[i].name + '</option>'
                }
                $("#leaveType").append(html);
                $("#leaveType").chosen({
                    maxHeight: 200
                });
                //点击事件
                $('#leaveType').on('change', function (e, params) {
                    $('#leaveTypeName').val($('#leaveType').find('option:selected').text());
                });
            } else {
                layer.msg(res.msg);
            }
        }
    });
}

function save() {
    $.ajax({
        cache: true,
        type: "POST",
        url: prefix + "/add",
        data: $('#leaveForm').serialize(),// 你的formid
        async: false,
        error: function () {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code === 0) {
                parent.layer.msg("添加成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                parent.layer.msg(data.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#leaveForm").validate({
        rules: {
            leaveStart: {
                required: true
            },
            leaveEnd: {
                required: true
            },
            remark: {
                required: true
            }
        },
        messages: {
            leaveStart: {
                required: icon + "还未选择开始时间！！"
            },
            leaveEnd: {
                required: icon + "还未选择结束时间！！"
            },
            remark: {
                required: icon + "还未填写备注信息！！"
            }
        }
    })
}
